package cemetery.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;

import cemetery.bean.account;
import cemetery.service.AccountService;
import cemetery.service.impl.AccountServiceImpl;

/**
 * Servlet implementation class UpdateServelt
 */
@WebServlet("/UpdateServelt")
public class UpdateServelt extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public UpdateServelt() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	response.setContentType("text/json;charset=utf-8");
	String action=request.getParameter("action");
	
	if("update".equals(action)) {
		String user=request.getParameter("user");
		String mpwd=request.getParameter("mpwd");
		String renewpwd=request.getParameter("renewpwd");
		account a=new account();
		a.setUser(user);
		a.setMpwd(mpwd);
		a.setPwd(renewpwd);
		AccountService asi=new AccountServiceImpl();
		int count= asi.update(a);
		 Map<String, Object> map = new HashMap<String, Object>();
			if (count> 0) {						
				map.put("msg", "成功");
				map.put("count", 1);
				
			} else {					
				map.put("msg", "修改失败,原始密码错误");
				map.put("count", -1);
			}
			PrintWriter pw = response.getWriter();
			String result = JSON.toJSONString(map);
			pw.write(result);
			pw.flush();// 刷新
			pw.close();
		
	}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
